import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:dio/dio.dart';

typedef OnMsgCallBack = Function(String data);

class SSE {
  ResponseBody data;
  SSE(this.data);
  _getUnit8Transformer() {
    StreamTransformer<Uint8List, List<int>> unit8Transformer =
        StreamTransformer.fromHandlers(
      handleData: (data, sink) {
        sink.add(List<int>.from(data));
      },
    );
    return unit8Transformer;
  }

  onMessage({required OnMsgCallBack onMsg}) {
    data.stream
        .transform(_getUnit8Transformer())
        .transform(const Utf8Decoder())
        .transform(const LineSplitter())
        .listen((event) {
      onMsg(event);
    });
  }
}
